package com.example.mybatis_demo.mapper;

import com.example.mybatis_demo.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import javax.swing.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-29
 * Time: 11:29
 */
@Mapper//交给Spring管理，类比五大注解
public interface UserInfoMapper {
    //自己的理解：Spring实现了这个接口，并重写了方法，并实例化了这样一个对象
    // 对象调用这个方法，达到select *查询这样一个功能
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag(Integer deleteFlag);

    //查询性别是男生1，delete_flag逻辑删除字段为0的数据
    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfo2(Integer deleteFlag , Integer gender);

    //返回的类型不在是一个集合，而是UserInfo类型，预计会返回2个对象，但是实际不是
    @Select("select * from userinfo where delete_flag = #{deleteFlag} and gender = #{gender}")
    UserInfo getUserInfo3(Integer deleteFlag , Integer gender);


//    增删查改的返回类型都是Integer类型，比如：新增行数1行，删除行数1行
//    into后面跟数据库中的参数，values后面的参数取值取决于类UserInfo中对象的值
    @Options(useGeneratedKeys = true , keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);


    //测试@Param参数绑定
    @Insert("insert into userinfo (username,password,age,gender) " +
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = (#{id})")
    Integer delete(Integer id);

    @Update("update userinfo set password = #{password } , age = #{age} , gender = #{gender} where id = #{id} ")
    Integer update(UserInfo userInfo);
    @Select("select id , username , password , age , gender , phone , " +
            "delete_flag as deleteFlag , create_time as createTime , update_time as updateTime " +
            "from userinfo")
    List<UserInfo> selectUserInfos();

//    column MySQL中的列‘property:Java对象的属性
//    实际开发过程中不适用select *这种写法，可读性差
//    定义results可以复用映射关系
    @Results(id = "resultMap" , value = {
            @Result(column = "delete_flag" , property = "deleteFlag"),
            @Result(column = "create_time" , property = "createTime"),
            @Result(column = "update_time" , property = "updateTime")
    })
    @Select("select id , username , password , age , gender , phone , " +
            "delete_flag as deleteFlag , create_time as createTime , update_time as updateTime " +
            "from userinfo")
    List<UserInfo> selectUserInfos2();

//    使用映射关系
    @ResultMap("resultMap")
    @Select("select id , username , password , age , gender , phone , " +
            "delete_flag as deleteFlag , create_time as createTime , update_time as updateTime " +
            "from userinfo")
    List<UserInfo> selectUserInfos3();

//    数据库字段规范，蛇形，单词之间_分割，全部小写
//    Java中为小驼峰
//    @Select("select id , username , password , age , gender , phone , " +
//            "delete_flag , create_time  , update_time " +
//            "from userinfo")


    @Select("select * from userinfo")
    List<UserInfo> selectUserInfos4();


    @Select("select * from userinfo where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoByDeleteFlag2(Integer deleteFlag);

    @Select("select * from userinfo where username = #{userName}")
    List<UserInfo> getUserByName(String userName);

    //#是预编译sql语句，$是拼接的
    @Select("select * from userinfo where username = '${userName}'")
    List<UserInfo> getUserByName2(String userName);

    @Select("select * from userinfo where username = #{username}")
    UserInfo selectUserByName(String userName);

    @Select("select * from userinfo where username = '${username}'")
    UserInfo selectUserByName2(String userName);

    @Select("select * from userinfo where username = '${username}' and password = '${password}' ")
    UserInfo queryUserBuNameAndPassword(String username, String password);

    @Select("select * from userinfo order by id ${order}")//天生我材必有用此处只能使用$选择，用#报错
    List<UserInfo> queryUserListByOrder(String order);

    @Select("select * from userinfo where username like '%${username}%'")//唯一破解，先拼接在占位.包含有这个参数的东西①先试$,②试#,③试给#加上''
    List<UserInfo> queryUserListByLike(String username);

    @Select("select * from userinfo where username like concat('%',#{username},'%')")//
    List<UserInfo> queryUserListByLike2(String username);

    @Select("select * from userinfo where username like concat('%','${username}','%')")//
    List<UserInfo> queryUserListByLike3(String username);


}
